﻿using System;
using System.Web;

namespace Presentation.Web.Core.CDN
{
    public class OutOfDateCondition : Intelligencia.UrlRewriter.IRewriteCondition
    {
        private readonly string _config;
        private static readonly System.Text.RegularExpressions.Regex Regex = 
            new System.Text.RegularExpressions.Regex(@"^~/CDN/\d+/(.+)$",
            System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        public OutOfDateCondition(string config)
        {
            _config = config;
        }

        #region IRewriteCondition Members

        public bool IsMatch(Intelligencia.UrlRewriter.RewriteContext context)
        {
            string path = MakeVirtualPathAppRelative(context.Location, HttpRuntime.AppDomainAppVirtualPath, true);
            var match = Regex.Match(path);
            if (match.Success)
            {
                string physicalPath = "~/" + match.Groups[1].Value;
                string currentPath = UrlProvider.Instance.GetCachableVirtualPath(physicalPath);
                if (!String.Equals(path, currentPath, StringComparison.OrdinalIgnoreCase))
                {
                    context.LastMatch = match;
                    return true;
                }
            }
            return false;
        }

        #endregion

        #region Helper Methods

        private static string MakeVirtualPathAppRelative(string virtualPath, string applicationPath, bool nullIfNotInApp)
        {
            if (virtualPath == null)
            {
                throw new ArgumentNullException("virtualPath");
            }
            int length = applicationPath.Length;
            int num2 = virtualPath.Length;
            if ((num2 == (length - 1)) && virtualPath.StartsWith(applicationPath, StringComparison.OrdinalIgnoreCase))
            {
                return "~/";
            }
            if (!VirtualPathStartsWithVirtualPath(virtualPath, applicationPath))
            {
                if (nullIfNotInApp)
                {
                    return null;
                }
                return virtualPath;
            }
            if (num2 == length)
            {
                return "~/";
            }
            if (length == 1)
            {
                return ('~' + virtualPath);
            }
            return ('~' + virtualPath.Substring(length));
        }

        private static bool VirtualPathStartsWithVirtualPath(string virtualPath1, string virtualPath2)
        {
            if (virtualPath1 == null)
            {
                throw new ArgumentNullException("virtualPath1");
            }
            if (virtualPath2 == null)
            {
                throw new ArgumentNullException("virtualPath2");
            }
            if (!virtualPath1.StartsWith(virtualPath2, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            int length = virtualPath2.Length;
            if (virtualPath1.Length != length)
            {
                if (length == 1)
                {
                    return true;
                }
                if (virtualPath2[length - 1] == '/')
                {
                    return true;
                }
                if (virtualPath1[length] != '/')
                {
                    return false;
                }
            }
            return true;
        }



        #endregion Helper Methods
    }

    public class OutOfDateConditionParser : Intelligencia.UrlRewriter.IRewriteConditionParser
    {

        #region IRewriteConditionParser Members

        public Intelligencia.UrlRewriter.IRewriteCondition Parse(System.Xml.XmlNode node)
        {
            var outOfDateAttr = node.Attributes.GetNamedItem("outOfDate");
            if (outOfDateAttr != null)
            {
                return new OutOfDateCondition(outOfDateAttr.Value);
            }

            return null;

        }

        #endregion
    }
}
